package com.aplink.generic.util.objectrule;

import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjPropInfo;

import java.util.ArrayList;

public class ActionCompareRule extends ActionBaseRule {

    public ActionCompareRule() {
        this.arrObjPropInfo = new ArrayList<ObjPropInfo>();
    }

    public ActionCompareRule( ArrayList<ObjPropInfo> _arrObjPropInfo ) {
        this.arrObjPropInfo = new ArrayList<ObjPropInfo>();
        this.arrObjPropInfo = _arrObjPropInfo;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public boolean isSatisfiedActionRule( GenericObject currentObject ) {

        int successCounter = 0;
        for ( final ObjPropInfo objPropInfo : this.arrObjPropInfo ) {
            // get Rule for this ObjPropInfo
            final ValueBaseRule valueBaseRule = objPropInfo.getValueBaseRule();

            // get Attribute of this ObjPropInfo from current GenericObject
            final GenericAttribute currentAttribute = currentObject
                    .getGenericAttributeFromObjectMappingName( objPropInfo
                            .getObjectProperty() );
            if ( currentAttribute != null ) {
                final boolean isSuccessful = valueBaseRule
                        .checkRule( currentAttribute );
                if ( isSuccessful ) {
                    successCounter++;
                }
            }
        }
        return successCounter == this.arrObjPropInfo.size();
    }

}
